package com.zsc.newbuds.controller.admin;

import com.zsc.newbuds.entity.Admin;
import com.zsc.newbuds.entity.ExchangeRecord;
import com.zsc.newbuds.entity.Shop;
import com.zsc.newbuds.entity.User;
import com.zsc.newbuds.service.impl.ExchangeRecordServiceImpl;
import com.zsc.newbuds.service.impl.ShopServiceImpl;
import com.zsc.newbuds.utils.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@Controller
public class ShopController {

    @Autowired
    ExchangeRecordServiceImpl exchangeRecordService;

    @Autowired
    OssUtil ossUtil;
    @Autowired
    ShopServiceImpl shopService;

    @ResponseBody
    @GetMapping("admin/shopManagement/goodsList")
    public Map getGoodsList(HttpServletRequest request){
        //查询所有商品数据
        //TODO:未完成根据limit显示数据，现仅能每页显示10条数据
        List<Shop> goodsList = shopService.getGoodsList();
        int page=Integer.parseInt(request.getParameter("page"));
        int limit=Integer.parseInt(request.getParameter("limit"));
        System.out.println("page="+page+",limit="+limit);
        System.out.println(goodsList);
        List<Shop> list;
        if(page == 1){
            list = goodsList.subList(0,limit);
        }else{
            list = goodsList.subList(limit,goodsList.size());
        }
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("count",goodsList.size());
        map.put("data",list);
        return map;
    }

    @ResponseBody
    @PostMapping("admin/shopManagement/addGoods")
    public int addGoods(HttpSession session,@RequestParam("image") MultipartFile image,String goodsName,
                        String goodsPrice,String goodsNumber) throws IOException {

        Admin admin = (Admin)session.getAttribute("admin");
        System.out.println(admin.getAdminName());
        System.out.println(image);
        System.out.println(goodsPrice);
        Shop shop = new Shop();
        String imageUrl = ossUtil.upLoad(image,"goods",admin.getAdminId());
        shop.setGoodsCount(Integer.parseInt(goodsNumber));
        shop.setGoodsImage(imageUrl);
        shop.setGoodsName(goodsName);
        shop.setGoodsOperator(admin.getAdminName());
        shop.setGoodsPrice(Integer.parseInt(goodsPrice));
        shopService.addGoods(shop);
        System.out.println("增加商品");
        return 1;
    }

    @ResponseBody
    @GetMapping("admin/shopManagement/getShopList")
    public List<Shop> getShopList(){
        return shopService.getGoodsList();
    }

    @ResponseBody
    @GetMapping("admin/shopManagement/getExchangeRecordList")
    public List<ExchangeRecord> getExchangeRecordList(){
        return exchangeRecordService.getExchangeRecordList();
    }
    @ResponseBody
    @GetMapping("user/getExchangeRecordList")
    public List<ExchangeRecord> getUserExchangeRecordList(HttpSession session){
        User user = (User)session.getAttribute("user");

        return exchangeRecordService.getUserExchangeRecordList(user.getUserId());
    }

    @ResponseBody
    @PostMapping("admin/shopManagement/provideNo")
    public void provideNo(Integer[] exchangeId){
        try{
            exchangeRecordService.provideNo(exchangeId);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @ResponseBody
    @PostMapping("admin/shopManagement/addGoodsCount")
    public int addGoodsCount(Integer[] goodsId,int count){
        try{
            System.out.println(Arrays.toString(goodsId));
            System.out.println(count);
            shopService.addGoodsCount(goodsId,count);
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

}
